package org.jeeasy.generate.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.dromara.mpe.autotable.annotation.Column;
import org.dromara.mpe.autotable.annotation.Table;
import org.jeeasy.common.core.base.BaseEntity;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 代码生成 模块
 *
 * @author AlpsDDJ
 * @date 2023/12/14
 */
@Data
@Table(value = "gen_module", comment = "模块")
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class GenModule extends BaseEntity<GenModule> {

    /**
     * 模块名称
     */
    @Column(comment = "模块名称", length = 32)
    private String name;

    /**
     * 路径
     */
    @Column(comment = "路径", length = 255)
    private String pkg;

    /**
     * 父包模块名
     */
    @Column(comment = "模块编码", length = 64)
    private String moduleCode;

    /**
     * Entity 路径
     */
    @Column(comment = "Entity 路径", length = 255)
    private String entity;

    /**
     * Mapper 路径
     */
    @Column(comment = "Mapper 路径", length = 255)
    private String mapper;

    /**
     * MapperXml 路径
     */
    @Column(comment = "MapperXml 路径", length = 255)
    private String mapperXml;

    /**
     * Service 路径
     */
    @Column(comment = "Service 路径", length = 255)
    private String service;

    /**
     * ServiceImpl 路径
     */
    @Column(comment = "ServiceImpl 路径", length = 255)
    private String serviceImpl;

    /**
     * controller 路径
     */
    @Column(comment = "Controller 路径", length = 255)
    private String controller;

    /**
     * webList 路径
     */
    @Column(comment = "webList 路径", length = 255)
    private String webList;

    /**
     * webModel 路径
     */
    @Column(comment = "webModel 路径", length = 255)
    private String webModel;

    //@Column(comment = "表id")
    //@TableField(exist = false)
    //private String tableId;

    public Map<String, String> pkgs() {
        return Map.of(
                "entity", pkg + "." + entity,
                "VO", pkg + "." + entity + ".vo",
                "DTO", pkg + "." + entity + ".dto",
                "mapper", pkg + "." + mapper,
                "mapperXml", pkg + "." + mapperXml,
                "service", pkg + "." + service,
                "serviceImpl", pkg + "." + serviceImpl,
                "controller", pkg + "." + controller,
                "webList", webList,
                "webModel", webModel
        );
    }

    public String pkg(String type, Map<String, String> replaceParams) {
        if (type != null) {
            AtomicReference<String> p = new AtomicReference<>(pkgs().get(type));
            replaceParams.forEach((key, value) -> p.set(p.get().replaceAll("\\{" + key + "}", value)));
            return p.get();
        }
        return null;
    }

}
